package spring.jpa.proxy

import org.springframework.stereotype.Component
import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method
import java.lang.reflect.Proxy

@Component
class JpaRepositoryFactory {
    fun createRepository(name: String): JpaRepository {
        return Proxy.newProxyInstance(JpaRepository::class.java.classLoader,
                arrayOf<Class<*>>(JpaRepository::class.java), JdkHandler(name)) as JpaRepository
    }

    inner class JdkHandler(val name: String) : InvocationHandler {
        @Throws(Throwable::class)
        override fun invoke(proxy: Any, method: Method, args: Array<Any>?): Any? {
            println(String.format("Call %s method %s", name, method.name))
            return null
        }
    }
}